package com.yanqu.road.dao.impl.activity.metaspace;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.metaspace.MetaspaceRewardData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class MetaSpaceRewardDaoImpl extends TempDataObjectDao<MetaspaceRewardData> {
    public static String getMetaspaceRewardDataSql = "select * from `t_u_meta_space` where `userId`=? and `activityId`=? ";
    public Map<Integer,MetaspaceRewardData> getMetaspaceRewardData(long userId,int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, MetaspaceRewardData> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getMetaspaceRewardDataSql);
            int i = 1;
            call.setLong(i++, userId);
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                MetaspaceRewardData data = parse(rs);
                dataMap.put(data.getId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateMetaspaceRewardDataSql = "update `t_u_meta_space` set reward=?,rewardTime=?,phoneMum=?,address=? where userId=? and activityId=? and id=?";
    @Override
    protected boolean updateData(MetaspaceRewardData metaspaceRewardData) {
        metaspaceRewardData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, metaspaceRewardData.getReward()));
        para.put(i++, new DbParameter(Types.BIGINT, metaspaceRewardData.getRewardTime()));
        para.put(i++, new DbParameter(Types.BIGINT, metaspaceRewardData.getPhoneMum()));
        para.put(i++, new DbParameter(Types.VARCHAR, metaspaceRewardData.getAddress()));
        para.put(i++, new DbParameter(Types.BIGINT, metaspaceRewardData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, metaspaceRewardData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, metaspaceRewardData.getId()));
        if (getDb().executeNoneQuery(updateMetaspaceRewardDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertMetaspaceRewardDataSql = "insert into `t_u_meta_space`(userId,activityId,id,reward,rewardTime,phoneMum,address) values (?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(MetaspaceRewardData metaspaceRewardData) {
        metaspaceRewardData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, metaspaceRewardData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, metaspaceRewardData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, metaspaceRewardData.getId()));
        para.put(i++, new DbParameter(Types.VARCHAR, metaspaceRewardData.getReward()));
        para.put(i++, new DbParameter(Types.BIGINT, metaspaceRewardData.getRewardTime()));
        para.put(i++, new DbParameter(Types.BIGINT, metaspaceRewardData.getPhoneMum()));
        para.put(i++, new DbParameter(Types.VARCHAR, metaspaceRewardData.getAddress()));
        if (getDb().executeNoneQuery(insertMetaspaceRewardDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected MetaspaceRewardData parseData(ResultSet rs) throws SQLException {
        MetaspaceRewardData metaspaceRewardData = new MetaspaceRewardData();
        metaspaceRewardData.setUserId(rs.getLong("userId"));
        metaspaceRewardData.setActivityId(rs.getInt("activityId"));
        metaspaceRewardData.setId(rs.getInt("id"));
        metaspaceRewardData.setReward(rs.getString("reward"));
        metaspaceRewardData.setRewardTime(rs.getLong("rewardTime"));
        metaspaceRewardData.setPhoneMum(rs.getLong("phoneMum"));
        metaspaceRewardData.setAddress(rs.getString("address"));

        return metaspaceRewardData;
    }
}
